﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

using System.Windows.Forms;
using NeuralNetwork;
using TheVoiceOfSnake;
using TheVoiceOfSnake.Models;
using TheVoiceOfSnake.Enums;

namespace DigitsRecognizer.UI
{
    public class PatternsForm : Form
    {
        private string Login = "test";
        private Label DescriptionLabel, CommandLabel, RecordingLabel;
        private Button SaveButton, CancelButton, StartButton, StopButton;
        public ComboBox ComboBox;
        private Writer<double> Writer;
        private string[] Commands;
        private string Parameters;
        SpeechRecognizer Recognizer;
        private int NumOfCommands = 0;

        public PatternsForm() 
        {
            FillConstans(); 
            this.Height = 225;
            this.Width = 300;
            this.CenterToScreen();
            this.MinimizeBox = false;
            this.MaximizeBox = false;
            this.Text = "Tworzenie zbioru treningowego";

            DescriptionLabel = new Label();
            DescriptionLabel.AutoSize = true;
            DescriptionLabel.Text = "Tworzenie zbioru treningowego";
            DescriptionLabel.Location = new Point(35, 10);
            DescriptionLabel.Font = new Font(new FontFamily(System.Drawing.Text.GenericFontFamilies.SansSerif), 10, FontStyle.Italic);

            CommandLabel = new Label();
            CommandLabel.AutoSize = true;
            CommandLabel.Text = "Komenda: ";
            CommandLabel.Location = new Point(60, 50);

            ComboBox = new ComboBox();
            ComboBox.Items.AddRange(Commands);
            ComboBox.Location = new Point(140, 47);
            ComboBox.IntegralHeight = false;
            ComboBox.MaxDropDownItems = 10;
            ComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
            ComboBox.Name = "Komendy";
            ComboBox.TabIndex = 0;
            ComboBox.Width = 80;

            RecordingLabel = new Label();
            RecordingLabel.AutoSize = true;
            RecordingLabel.Text = "Nagrywanie: ";
            RecordingLabel.Location = new Point(50, 85);

            StartButton = new Button();
            StartButton.Text = "Start";
            StartButton.Location = new Point(140, 82);
            StartButton.Width = 50;

            StopButton = new Button();
            StopButton.Text = "Stop";
            StopButton.Location = new Point(200, 82);
            StopButton.Width = 50;
            StopButton.Enabled = false;

            SaveButton = new Button();
            SaveButton.Text = "Zapisz";
            SaveButton.Location = new Point(22, 142);
            SaveButton.Enabled = false;

            CancelButton = new Button();
            CancelButton.Text = "Powrót";
            CancelButton.Location = new Point(182, 142);

            this.Controls.Add(DescriptionLabel);
            this.Controls.Add(CommandLabel);
            this.Controls.Add(RecordingLabel);
            this.Controls.Add(ComboBox);
            this.Controls.Add(SaveButton);
            this.Controls.Add(StopButton);
            this.Controls.Add(StartButton);
            this.Controls.Add(CancelButton);

            /* obsługa wciśnięcia przycisku Cancel */
            EventHandler cancelButtonEH = new EventHandler(OnClickCancel);
            CancelButton.Click += cancelButtonEH;

            /* obsługa wciśnięcia przycisku Start */
            EventHandler startButtonEH = new EventHandler(OnClickStart);
            StartButton.Click += startButtonEH;

            /* obsługa wciśnięcia przycisku Stop */
            EventHandler stopButtonEH = new EventHandler(OnClickStop);
            StopButton.Click += stopButtonEH;

            /* obsługa wciśnięcia przycisku Save */
            EventHandler saveButtonEH = new EventHandler(OnClickSave);
            SaveButton.Click += saveButtonEH;

            Recognizer = new SpeechRecognizer(this);
            Recognizer.IsGameMode(false);
            Recognizer.SetLogin(Login);
        }

        public PatternsForm(string login) : this()
        {
            Login = login;
        }

        private void OnClickCancel(object sender, EventArgs args)
        {
            this.Close();
        }

        private void OnClickStart(object sender, EventArgs args)
        {
            int selectedValue = Convert.ToInt32(ComboBox.SelectedIndex);
            if (selectedValue == -1)
            {
                MessageBox.Show("Wybierz komendę!", "Brak wybranej komendy z listy", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                Recognizer.AddCommandValue(TheVoiceOfSnake.Models.Commands.CommandsDct[TheVoiceOfSnake.Models.Commands.TransformCommandToDirection(ComboBox.Text)]);
                Recognizer.StartCapturing();
                Recognizer.Start();
                ElementsAfterStart();
            }
        }

        private void OnClickSave(object sender, EventArgs args)
        {
            Writer.SavaParams(Parameters, TheVoiceOfSnake.Models.Commands.CommandsDct[TheVoiceOfSnake.Models.Commands.TransformCommandToDirection(ComboBox.Text)], Login);
            MessageBox.Show(string.Format("Zarejestrowano {0} komend. Komendy dodano do zbioru testowego.", NumOfCommands), "Zapis zarejestrowanych komend", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Parameters = string.Empty;
            NumOfCommands = 0;
            SaveButton.Enabled = false;
        }

        private void OnClickStop(object sender, EventArgs args)
        {
            Recognizer.Stop();
            Recognizer.StopCapturing();
            ElementsAfterStop();
        }

        private void FillConstans() 
        {
            Writer = new Writer<double>();
            Commands = new string[TheVoiceOfSnake.Models.Commands.CommandsDct.Count-1];
            int i=0;
            foreach (var command in TheVoiceOfSnake.Models.Commands.CommandsDct) 
            {
                if (command.Key != Direction.None)
                {
                    Commands[i] = TheVoiceOfSnake.Models.Commands.TransformCommandToString(command.Key);
                    i++;
                }
            }
        }

        public void AddParametersString(string newParameters)
        {
            Parameters += newParameters;
            NumOfCommands++;
        }

        public void onClickExtract(object sender, EventArgs args)
        {
            Recognizer.StartCapturing();
            Recognizer.Start();
        }

        private void ElementsAfterStart() 
        {
            ComboBox.Enabled = false;
            SaveButton.Enabled = false;
            StartButton.Enabled = false;
            StopButton.Enabled = true;
        }

        private void ElementsAfterStop()
        {
            ComboBox.Enabled = true;
            SaveButton.Enabled = true;
            StartButton.Enabled = true;
            StopButton.Enabled = false;
        }
    }
}
